# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request
from ..items import ToscrapebookItem
from scrapy.loader import ItemLoader

class BooksSpider(scrapy.Spider):
    name = 'books'
    allowed_domains = ['books.toscrape.com']
    start_urls = ['http://books.toscrape.com/']

    def parse(self, response):
        item = ToscrapebookItem()
        for book in response.xpath('//article'):
            item['bookname'] = book.xpath('./h3/a/@title').extract()
            item['bookprice'] = book.xpath(".//p[@class='price_color']/text()").extract()
            yield item
        nex_url = response.xpath("//li[@class='next']/a/@href").extract_first()
        if nex_url:
            print(nex_url)
            nex_url = response.urljoin(nex_url)
            yield scrapy.Request(nex_url, callback=self.parse)
